package com.shuwei.wificollecttool;

/**
 * Created by Halohoop on 2017/11/22.
 */

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;

public class ArithmeticWriteSheet {

    public static void writeMap(File file, Map<String, Object[]> rssiMap,
                                int startRowId, boolean isNewFile) throws Exception {
        //Create blank workbook
        HSSFWorkbook workbook;
        HSSFSheet spreadsheet;
        FileInputStream fileInputStream = null;
        if (isNewFile) {
            //get a new sheet
            workbook = new HSSFWorkbook();
            spreadsheet = workbook.createSheet("算法验证");
        } else {
            //get a old sheet
            fileInputStream = new FileInputStream(file);
            workbook = new HSSFWorkbook(fileInputStream);
            spreadsheet = workbook.getSheet("算法验证");
        }

        //Create row object
        HSSFRow row;

        //Iterate over data and write to sheet
        Set<String> keyid = rssiMap.keySet();
        int rowid = startRowId;

        for (String key : keyid) {
            row = spreadsheet.createRow(rowid++);
            Object[] objectArr = rssiMap.get(key);
            int cellid = 0;

            for (Object obj : objectArr) {
                Cell cell = row.createCell(cellid++);
                cell.setCellValue("" + obj);
            }
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }

        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);
        out.close();
    }
}
